package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.VideoInfoForm;
import com.atguigu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
@Api(description = "小节管理")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    //注入服务调用的删除小节视频接口
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        System.out.println(eduVideo);
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //根基小节id删除小节 todo 在加入视频后需要改善 已完成
    @DeleteMapping("{id}")
    public R deleteVideoById(@PathVariable String id){
        //根据小节id获取 视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        //获取 视频id
        String videoSourceId = eduVideo.getVideoSourceId();

        if(!StringUtils.isEmpty(videoSourceId)){
            vodClient.deleteAliyunVideo(videoSourceId);
        }

        eduVideoService.removeById(id);
        return R.ok();
    }



    //根据id获取信息
    @GetMapping("getVideoById/{id}")
    public R getVideoById(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video",video);
    }

    //修改小节信息
    @PostMapping("updateVideo")
    public R updateVideoInfoForm(@RequestBody EduVideo eduVideo){
        Boolean flag = eduVideoService.updateById(eduVideo);
        System.out.println(eduVideo);
        return R.ok();
    }

}

